package com.didi.gulimall.product.controller;

import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.product.entity.BrandEntity;
import com.didi.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author didi
 * @email 1062664904@qq.com
 * @date 2021-01-14 15:42:45
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("infos")
    public R infoList(@RequestParam("brandIds") List<Long> brands) {
        List<BrandEntity> brand = brandService.infoList(brands);
        return R.ok().put("brand", brand);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);
        // TODO 判断该分类是否被引用 被引用不能修改名称
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@Validated @RequestBody BrandEntity brand) {
        brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody @Valid BrandEntity brand) {

        brandService.updateBrand(brand);

        return R.ok();
    }

    /**
     * 修改状态
     *
     * @param brand
     * @return
     */
    @PostMapping("/update/status")
    public R updateStatus(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
